package com.anrongtec.zcpt.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/21.
 */

public class JsonParse {

	public static <T> List<T> jsonToBeanList(String json, Class<T> t) {
		Gson gson = new Gson();
		List<T> list = new ArrayList<>();
		JsonParser parser = new JsonParser();
		JsonArray jsonarray = parser.parse(json).getAsJsonArray();
		for (JsonElement element : jsonarray
				) {
			list.add(gson.fromJson(element, t));
		}
		return list;
	}
	// 将Json数据解析成相应的映射对象
	public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
		Gson gson = new Gson();
		T result = gson.fromJson(jsonData, type);
		return result;
	}
}
